<?php
namespace zzcms\primary\controller;


class Apply extends Base
{
    protected function initialize()
    {
        parent::initialize();
    }

    public function pos()
    {
        $model = new \zzcms\admin\model\Apply();
        $data = json2array(htmlspecialchars_decode($this->info['message']));
        if (isset($data['areaids']) && !isset($data['province_id'])) {
            //此为VUE传来的信息
            $data['province_id']=$data['areaids'][0];
            $data['city_id']=$data['areaids'][1];
            $data['area_id']=$data['areaids'][2];
        }
        if (false === $model->addData($data)) {
            $this->array2json(['code' => 1, 'data' => $model->getError()]);
        } else {
            $this->array2json(['code' => 0, 'data' => 'ok']);
        }
    }

    public function catetree()
    {
        $areas = getCache('linkage');
        $trees = $this->getCateTree($areas);
        $this->array2json(['code' => 0, 'data' => $trees]);
    }

    /**
     * 格式化分类为树形结构
     * @param $items
     * @return array
     */
    private function getCateTree($items)
    {
        $tree = []; //初始化格式化好的树
        foreach ($items as $item) {
            if (isset($items[$item['parentid']])) {
                $items[$item['parentid']]['children'][] = &$items[$item['catid']];
            } else {
                $tree[] = &$items[$item['catid']];
            }
        }
        return $tree;
    }
}
